package com.sdry.service.impl.llm;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.llm.PermissionMapper;
import com.sdry.mapper.llm.RoleMapper;
import com.sdry.model.llm.Permission;
import com.sdry.model.llm.Role;
import com.sdry.model.llm.RolePermission;
import com.sdry.model.llm.TreeBean;
import com.sdry.model.llm.UserRole;
import com.sdry.service.llm.RoleService;
import com.sdry.utils.TreeUtils;

/** 
* 角色service实现类
* @author lian 
* @version 1.0
* @time 2018年10月17日 上午12:56:23
*/

@Service
public class RoleServiceImp implements RoleService {

	@Resource
	private RoleMapper roleMapper; //注入角色mapper
	@Resource
	private PermissionMapper permissionMapper; //注入角色mapper
	
	/**
	 * 获取角色总数
	 * @param role 角色信息实体类
	 */
	@Override
	public int countRole(Role role) {
		return roleMapper.countRole(role);
	}

	/**
	 * 分页查询角色信息
	 * @param map 查询条件集合
	 */
	@Override
	public List<Role> listPageRole(Map<String, Object> map) {
		return roleMapper.listPageRole(map);
	}

	/**
	 * 保存角色信息
	 * @param role 角色信息实体类
	 */
	@Override
	public int insertRole(Role role) {
		int isSuccess = -2;
		//添加创建日期
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(date);
		role.setCreateTime(time);
		List<Role> list = roleMapper.getRoleByName(role.getName());
		if(list.size() == 0){
			int i = roleMapper.insertRole(role);
			if(i > 0){
				isSuccess = 2;
			}
		}
		return isSuccess;
	}

	/**
	 * 修改角色信息
	 * @param role 角色信息实体类
	 */
	@Override
	public int updateRole(Role role) {
		int isSuccess = -3;
		Role ro = roleMapper.getRoleById(role.getRid());
		if(ro.getName().equals(role.getName())){
			int i = roleMapper.updateRole(role);
			if(i > 0){
				isSuccess = 3;
			}
		}else{
			List<Role> list = roleMapper.getRoleByName(role.getName());
			if(list.size() == 0){
				int i = roleMapper.updateRole(role);
				if(i > 0){
					isSuccess = 3;
				}
			}
		}
		return isSuccess;
	}

	/**
	 * 删除角色
	 * @param str rid组成的字符串
	 */
	@Transactional(rollbackFor={Exception.class})
	public int deleteRole(String str) {
		int isSuccess = 0;
		String[] split = str.split(",");
		try {
			for (String rid : split) {
				isSuccess = roleMapper.deleteRole(Long.parseLong(rid));
			}
		}catch (
		Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return isSuccess;
	}

	/**
	 * 根据角色ID获取权限(树结构)
	 * @param rid 角色ID
	 * @param response
	 */
	@Override
	public List<TreeBean> getPermissionTree(Long rid) {
		List<Permission> all = permissionMapper.findAllPermission();
		List<Permission> has = permissionMapper.findPermissionByRoleId(rid);
		return TreeUtils.permission2tree(all, has);
	}

	/**
	 * 角色授权
	 * @param pids 权限ID组成的字符串
	 * @param response
	 */
	@Transactional(rollbackFor={Exception.class})
	public int accreditUser(String pids, String rid) {
		int isSuccess = 0;
		String[] split = pids.split(",");
		RolePermission rp = new RolePermission();
		rp.setRid(Long.parseLong(rid));
		try {
			// 在关系表中删除对应的角色数据
			roleMapper.deletePermissionByRole(Long.parseLong(rid));
			//如果有选择的资源ID
			if(pids.length() > 0){
				// 遍历要授予的角色的ID
				for (String pid : split) {
					rp.setPid(Long.parseLong(pid));
					// 把数据存到用户和角色关系表中
					isSuccess = roleMapper.accreditRole(rp);
				}
			}else{
				isSuccess = 0;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return isSuccess;
	}

	/**
	 * 所有权限信息
	 */
	@Override
	public List<Permission> findAllPermission() {
		return permissionMapper.findAllPermission();
	}

	/**
	 * 根据角色ID获取权限(数据库表形式)
	 * @param rid 角色ID
	 * return
	 */
	@Override
	public List<Permission> getPermissionByRole(long rid) {
		return permissionMapper.findPermissionByRoleId(rid);
	}

	/**
	 * 校验是否删除角色
	 * @param str
	 * @return
	 */
	@Override
	public int deleteRoleOfVerify(String str) {
		int isSuccess = -1;
		String[] split = str.split(",");
		for(int i = 0; i < split.length; i ++){
			Long id = Long.parseLong(split[i]);
			List<UserRole> urList = roleMapper.getUserRoleByRid(id);
			if(urList.size() > 0){
				isSuccess = 1;
				break;
			}
		}
		return isSuccess;
	}

}
